package com.glls.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

/**
 * TODO
 *
 * @author junyang
 * @version 1.0.0
 * @date 2025/2/21 16:27
 */

@Component
public class CalServiceTimeGatewayFilterFactory extends AbstractGatewayFilterFactory<MyConfig> {

    public CalServiceTimeGatewayFilterFactory() {
        super(MyConfig.class);
    }






    @Override
    public GatewayFilter apply(MyConfig config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                //前处理
                long startTime = System.currentTimeMillis();

                System.out.println("name:"+config.getKey());
                System.out.println("value:"+config.getValue());

                // return chain.filter(exchange);//放⾏

                return chain.filter(exchange).then(
                        //后置处理
                        Mono.fromRunnable(()->{
                            System.out.println("post come in");
                            //获取系统当前时间戳为endTime
                            long entTime = System.currentTimeMillis();
                            System.out.println("接口响应时间time="+(entTime-startTime));

                        }));
            }
        };
    };


    @Override
    public List<String> shortcutFieldOrder() {
        //- MyHeader=bbb,cccc
        //[bbb,cccc]
        //bbb赋值给MyConfig#key
        //cccc赋值给MyConfig#value

        return Arrays.asList("key","value");
    }


}
